﻿using Consul;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace  Microsoft.Extensions; 
public static class ConsulExtensions
{
    public static IApplicationBuilder RegisterConsul(this IApplicationBuilder app, IConfiguration configuration)//, IHostApplicationLifetime lifetime)
    {
        var consulClient = new ConsulClient(c =>
        {
            c.Address = new Uri(configuration["ConsulSetting:ConsulAddress"]);
        });

        var registration = new AgentServiceRegistration()
        {
            ID = Guid.NewGuid().ToString(),//服务实例唯一标识
            Name = configuration["ConsulSetting:ServiceName"],//服务名
            Address = configuration["ConsulSetting:ServiceIP"], //服务IP
            Port = int.Parse(configuration["ConsulSetting:ServicePort"]),//服务端口 因为要运行多个实例，端口不能在appsettings.json里配置，在docker容器运行时传入
            Check = new AgentServiceCheck()
            {
                DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(5),//服务启动多久后注册
                Interval = TimeSpan.FromSeconds(10),//健康检查时间间隔
                HTTP = $"http://{configuration["ConsulSetting:ServiceIP"]}:{configuration["ConsulSetting:ServicePort"]}{configuration["ConsulSetting:ServiceHealthCheck"]}",//健康检查地址
                Timeout = TimeSpan.FromSeconds(5)//超时时间
            }
        };

        //服务注册
        consulClient.Agent.ServiceRegister(registration).Wait();
        
        // await consulClient.Agent.ServiceDeregister(registration.ID);
        // await consulClient.Agent.ServiceRegister(registration);

        ////应用程序终止时，手动取消注册
        //lifetime.ApplicationStopping.Register(() =>
        //{
        //    consulClient.Agent.ServiceDeregister(registration.ID).Wait();
        //});

        return app;
    }
}